<template>
	<view>
		<!-- 吸顶效果的搜索组件 -->
		<view class="search-box">
			<uni-search-bar :radius="100" @input="input" cancelButton="none"></uni-search-bar>
		</view>
		
		<!-- 搜索结果区域 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item , index) in searchResults" :key="item.goods_id" @click="gotoDetail(item)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史区域 -->
		<view class="history-box" v-else>
			<!-- 标题区 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<!-- 列表区 -->
			<view class="history-list">
				<view class="history-item" v-for="(item,index) in histories" :key="index" @click="gotoGoodsList(item)">
					<uni-tag :text="item"></uni-tag>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//延时器的 timer
				timer: null,
				//搜索关键词
				kw: '',
				//搜索建议结果列表数据
				searchResults: [],
				//搜索关键词的历史记录
				historyList: [],
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		computed: {
			histories() {
				return [...this.historyList].reverse()
			}
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e ;
					this.getSearchList();
				}, 500)
			},
			async getSearchList() {
				//判断搜索关键词是否为空
				if(this.kw.length === 0) {
					this.searchResults = [];
					return
				}
				this.saveSearchHistory();
				const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query: this.kw})
				if(res.meta.status !== 200) return uni.$showMsg();
				this.searchResults = res.message ;
				
			},
			//保存关键词的方法
			saveSearchHistory() {
				this.historyList.push(this.kw);
				let set_history = new Set(this.historyList);
				set_history.delete(this.kw);
				set_history.add(this.kw);
				this.historyList = Array.from(set_history);
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			//清空历史jil 
			clean() {
				this.historyList = [];
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			gotoDetail(item) {
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			gotoGoodsList(kw) {
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		//设置定位效果为: "吸顶"
		position: sticky;
		//吸顶的位置
		top: 0;
		//提高层级，防止被轮播图覆盖
		z-index: 999;
	}
	.sugg-list {
		padding: 0 5px;
		
		.sugg-item {
			display: flex;
			// flex-wrap: nowrap;
			justify-content: space-between;
			align-items: center;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px dashed #efefef;
			
			.goods-name {
				//文本不允许换行（单行文本）
				white-space: nowrap;
				//溢出部分隐藏
				overflow: hidden;
				//文本溢出后，使用 ... 代替
				text-overflow: ellipsis;
				margin-right: 6px;
			}
		}
		.sugg-item:last-child {
			border-bottom: none;
		}
	}
	
	.history-box {
		padding: 0 5px;
		
		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}
		
		.history-list {
			display: flex;
			flex-wrap: wrap;
			
			.history-item{
				margin: 5px 5px 0 0;
				
				.uni-tag {
					background: #FFFFFF;
					border-color: #efefef;
					color: #333333;
				}
			}
		}
	}
</style>
